# _*_ coding : utf-8 _*_
# @Time : 2023/12/4 9:24
# @Author : 韩宗森
# @File : 7_post请求百度翻译
# @Project : pythonProject

import urllib.request
import urllib.parse
import json

url = 'https://fanyi.baidu.com/v2transapi?from=id&to=zh'

# 定制浏览器信息
headers = {

    'Cookie':'BIDUPSID=28567312798ABEF7CE3D142C864AFCFE; PSTM=1697164255; BAIDUID=28567312798ABEF7A0FCA2314D3F75DB:FG=1; APPGUIDE_10_6_6=1; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDUSS=d0NWswVkhEUEN1M1ltUUxHMElPVkliS0JRMVphQU5YbkZDLVJEWlphQ2NFMk5sSVFBQUFBJCQAAAAAAAAAAAEAAABmSdRPwdnQ0s7Sz8LD5sz1AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJyGO2Wchjtlaj; BDUSS_BFESS=d0NWswVkhEUEN1M1ltUUxHMElPVkliS0JRMVphQU5YbkZDLVJEWlphQ2NFMk5sSVFBQUFBJCQAAAAAAAAAAAEAAABmSdRPwdnQ0s7Sz8LD5sz1AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJyGO2Wchjtlaj; APPGUIDE_10_6_7=1; APPGUIDE_10_6_9=1; H_WISE_SIDS=110085_275733_278414_259642_280650_281808_281865_281893_281686_281830_281996_282195_279009_282058_282996_283165_283633_281704_281052_283726_283718_279613_283766_203518_283886_284024_278388_284281_284283_8000088_8000105_8000114_8000124_8000138_8000150_8000162_8000165_8000169_8000177_8000176_8000183_8000186_8000189; H_WISE_SIDS_BFESS=110085_275733_278414_259642_280650_281808_281865_281893_281686_281830_281996_282195_279009_282058_282996_283165_283633_281704_281052_283726_283718_279613_283766_203518_283886_284024_278388_284281_284283_8000088_8000105_8000114_8000124_8000138_8000150_8000162_8000165_8000169_8000177_8000176_8000183_8000186_8000189; newlogin=1; MCITY=-261%3A; BAIDUID_BFESS=28567312798ABEF7A0FCA2314D3F75DB:FG=1; ZFY=dnwwFdIHtJTMsNTrllvQH79qmdti3PSGsBlpdcf9sfY:C; BA_HECTOR=aha1al8h81ag0h8ha0a424871imreum1q; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; __bid_n=18c0e7157c00144d55b70b; RT="z=1&dm=baidu.com&si=cb3ca029-9bba-475b-8a49-9c457095d7ec&ss=lpqwk4hc&sl=0&tt=0&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ul=47h&hd=4kl"; H_PS_PSSID=39678_39713_39738_39751_39703_39682_39662_39818_39841_39904; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; PSINO=7; delPer=0; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1701394553,1701601245,1701652798,1701747590; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1701747590; ab_sr=1.0.1_YzY5NjA3MjgwODQwOTVjNTE4MGIwNDNkZWYxNDg5ODgyNDg4MWZhNzI0YzkyMmIxMGM0NWUxZmUxM2NhNmQzMzM3MDk2ZTA2MzA3Zjc3ZjZhNTE1ODQwNzE0MmU0ZjU0M2I4ZmY2N2ZmNmU1ZWRkNWE1ZDM4ZTYyODgyOGUxZjgyMDAwZjBhYjMwOGY2NzJkMzk1OTdkNTg3OWQ4Zjc5YjRlY2Y1NGE5ZTNkNTY4M2U5NTlmOGY3NGUxMzdkNzQ0'
}

# 请求数据
data = {
    'from': 'en',
    'to': 'zh',
    'query': 'teacher',
    'transtype': 'realtime',
    'simple_means_flag': '3',
    'sign': '384259.81458',
    'token': '094807290fe2fd41b1ea23d0f41c6502',
    'domain': 'common',
    'ts': '1701747886481'
}

# post请求的参数必须要进行编码 并且要调用encode方法
data = urllib.parse.urlencode(data).encode('utf-8')

# post的请求的参数是不会拼接在url的后面的﹑而是需要放在请求对象定制的参数中
request = urllib.request.Request(url=url, data=data, headers=headers)

# 模拟浏览器向服务器发送请求
response = urllib.request.urlopen(request)

# 获取响应的数据
content = response.read().decode('utf-8')

# 字符串--》json对象
obj = json.loads(content)

print(obj)

# post请求方式的参数必须编码data = urllib.parse.urlencode(data)
# 编码之后必须调用encode方法 data = urllib.parse.urlencode( data ) .encode( 'utf-8
# 参数是放在请求对象定制的方法中request = urllib.request.Request(url=url, data=da
